package com.michale.admin.controller.system;

import com.github.pagehelper.PageInfo;
import com.michale.admin.controller.tool.BaseController;
import com.michale.common.annotation.Log;
import com.michale.common.domain.system.DictDataEntity;
import com.michale.common.enmus.BusinessType;
import com.michale.common.result.AjaxResult;
import com.michale.common.result.PageResult;
import com.michale.common.utils.String.StringUtils;
import com.michale.system.service.DictDataService;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Michale
 * @apiNote 字典数据管理
 */
@Api(tags = "字典数据管理")
@RestController
@RequestMapping("system/dict/data")
public class DictDataController extends BaseController {

    @Resource
    private DictDataService dictDataService;

    /**
     * 分页查询-字典数据
     *
     * @param dictDataEntity 查询条件
     * @return 字典数据列表
     */
    @GetMapping("/dictDataList")
    @PreAuthorize("@security.isPermission('system:dict:list')")
    public PageResult getDictDataList(DictDataEntity dictDataEntity) {
        startPage();
        List<DictDataEntity> dictDataList = dictDataService.selectDictDataList(dictDataEntity);
        PageInfo<DictDataEntity> pageInfo = new PageInfo<>(dictDataList);
        return pageResult(pageInfo);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @GetMapping("/{dictCode}")
    @PreAuthorize("@security.isPermission('system:dict:query')")
    public DictDataEntity getDictDataInfo(@PathVariable Long dictCode) {
        return dictDataService.selectDictDataById(dictCode);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/dictType/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType) {
        List<DictDataEntity> dictDataEntityList = dictDataService.selectDictDataByType(dictType);
        if (StringUtils.isNull(dictDataEntityList)) {
            dictDataEntityList = new ArrayList<>();
        }
        return AjaxResult.success(dictDataEntityList);
    }

    /**
     * 新增字典数据信息
     *
     * @param dictDataEntity 字典数据信息
     * @return 结果
     */
    @PostMapping("/insertDictData")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PreAuthorize("@security.isPermission('system:dict:add')")
    public AjaxResult insertDictData(@Validated @RequestBody DictDataEntity dictDataEntity) {
        return toAjax(dictDataService.insertDictData(dictDataEntity));
    }

    /**
     * 修改字典数据信息
     *
     * @param dictDataEntity 字典数据信息
     * @return 结果
     */
    @PutMapping("/updateDictData")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PreAuthorize("@security.isPermission('system:dict:edit')")
    public AjaxResult updateDictData(@Validated @RequestBody DictDataEntity dictDataEntity) {
        return toAjax(dictDataService.updateDictData(dictDataEntity));
    }

    /**
     * 根据字典数据ID删除字典数据
     *
     * @param dictCodes 需要删除的字典数据ID
     * @return 结果
     */
    @DeleteMapping("/deleteDictData/{dictCodes}")
    @PreAuthorize("@security.isPermission('system:dict:remove')")
    public AjaxResult deleteDictData(@PathVariable Long[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }
}
